package cn.gemiman.daoyi.module.iot.service.kafkamsglogs;

import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.module.iot.controller.admin.kafkamsglogs.vo.KafkaMsgLogsPageReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.kafkamsglogs.vo.KafkaMsgLogsSaveReqVO;
import cn.gemiman.daoyi.module.iot.dal.dataobject.kafkamsglogs.KafkaMsgLogsDO;
import cn.gemiman.daoyi.module.iot.dal.mysql.kafkamsglogs.KafkaMsgLogsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.gemiman.daoyi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gemiman.daoyi.module.iot.enums.ErrorCodeConstants.KAFKA_MSG_LOGS_NOT_EXISTS;

/**
 * kafka原始消息 Service 实现类
 *
 * @author 道一源码
 */
@Service
@Validated
public class KafkaMsgLogsServiceImpl implements KafkaMsgLogsService {

    @Resource
    private KafkaMsgLogsMapper kafkaMsgLogsMapper;

    @Override
    public Long createKafkaMsgLogs(KafkaMsgLogsSaveReqVO createReqVO) {
        // 插入
        KafkaMsgLogsDO kafkaMsgLogs = BeanUtils.toBean(createReqVO, KafkaMsgLogsDO.class);
        kafkaMsgLogsMapper.insert(kafkaMsgLogs);
        // 返回
        return kafkaMsgLogs.getId();
    }

    @Override
    public void updateKafkaMsgLogs(KafkaMsgLogsSaveReqVO updateReqVO) {
        // 校验存在
        validateKafkaMsgLogsExists(updateReqVO.getId());
        // 更新
        KafkaMsgLogsDO updateObj = BeanUtils.toBean(updateReqVO, KafkaMsgLogsDO.class);
        kafkaMsgLogsMapper.updateById(updateObj);
    }

    @Override
    public void deleteKafkaMsgLogs(Long id) {
        // 校验存在
        validateKafkaMsgLogsExists(id);
        // 删除
        kafkaMsgLogsMapper.deleteById(id);
    }

    private void validateKafkaMsgLogsExists(Long id) {
        if (kafkaMsgLogsMapper.selectById(id) == null) {
            throw exception(KAFKA_MSG_LOGS_NOT_EXISTS);
        }
    }

    @Override
    public KafkaMsgLogsDO getKafkaMsgLogs(Long id) {
        return kafkaMsgLogsMapper.selectById(id);
    }

    @Override
    public PageResult<KafkaMsgLogsDO> getKafkaMsgLogsPage(KafkaMsgLogsPageReqVO pageReqVO) {
        return kafkaMsgLogsMapper.selectPage(pageReqVO);
    }

}
